<?php

class CommissionController extends AppController {

	var $paginate = array(
		'Commission' => array('order' => 'Commission.id DESC', 'limit' => 10)
	);

	public function index() {
		$parameters = $this->getParameters();
		$this->set('commissions', $this->getListCommission($parameters['year'], $parameters['month'], $parameters['type']));
		$this->set('totalCommission', $this->getTotalCommission($parameters['year'], $parameters['month'], $parameters['type']));
	}

	public function stats() {
		$parameters = $this->getParameters();
		$this->set('totalCommission', $this->getTotalCommission($parameters['year'], $parameters['month'], $parameters['type']));
	}

	protected function getParameters() {
		$statsType = 'total';
		$year = NULL;
		$month = NULL;

		if ($this->request->params['pass']) {
			$parameters = $this->request->params['pass'];

			if (isset($parameters[0])) {
				$statsType = $parameters[0];
			}
			if (isset($parameters[1])) {
				$year = $parameters[1];
			}
			if (isset($parameters[2])) {
				$month = $parameters[2];
			}
		}

		if ($statsType == 'net') {
			$personType = array(PersonType::PT_ADMIN, PersonType::PT_T1, PersonType::PT_T2, PersonType::PT_T3);
		} elseif ($statsType == 'broker') {
			$personType = PersonType::PT_BROKER;
		} elseif ($statsType == 'companies') {
			$personType = PersonType::PT_F3;
		} elseif ($statsType == 'company') {
			$personType = PersonType::PT_COMPANY;
		} else {
			$personType = array();
		}

		$this->set('years', $this->getYears());
		$this->set('actualType', $statsType);
		$this->set('actualYear', $year);
		$this->set('actualMonth', $month);

		return array('type' => $personType, 'year' => $year, 'month' => $month);
	}

	protected function getTotalCommission($year = NULL, $month = NULL, $personType = array()) {
		$conditions = $this->getConditions($year, $month, $personType);
		$commissions = $this->Commission->find('first', array('fields' => array('SUM(Commission.value) AS commission_sum'), 'conditions' => $conditions));
		return floatval($commissions[0]['commission_sum']);
	}

	protected function getListCommission($year = NULL, $month = NULL, $personType = array()) {
		$conditions = $this->getConditions($year, $month, $personType);
		$commissions = $this->paginate($conditions);
		return $commissions;
	}

	protected function getConditions($year = NULL, $month = NULL, $personType = array()) {
		$conditions = array();
		if ($year !== NULL) {
			$startMonth = 1;
			$endMonth = 12;
			if ($month !== NULL) {
				$startMonth = $month;
				$endMonth = $month;
			}
			$firstDayOfStats = mktime(0, 0, 0, $startMonth, 1, $year);
			$lastDayOfStats = mktime(23, 59, 59, $endMonth, 31, $year);

			$conditions[] = 'Commission.create_date BETWEEN ' . $firstDayOfStats . ' AND ' . $lastDayOfStats;
		}

		if ($personType) {
			$conditions['Commission.fk_person_type_id'] = $personType;
		}
		return $conditions;
	}
	
	protected function getYears() {
		$minimax = $this->Commission->find('first', array('fields' => array('MIN(Commission.create_date) AS minimum', 'MAX(Commission.create_date) AS maximum')));

		$minYear = date('Y', $minimax[0]['minimum']);
		$maxYear = date('Y', $minimax[0]['maximum']);

		$years = array();
		for ($i = $maxYear; $i >= $minYear; $i--) {
			$years[] = $i;
		}
		return $years;
	}
	
	public function delete($id = null) {
		$this->Commission->id = $id;
		if (!$this->Commission->exists()) {
			$this->setFlashError('Záznam nebol nájdený.');
		} else {
			if ($this->Commission->delete()) {
				$this->setFlashSuccess('Záznam bol zmazaný z databázy.');
			} else {
				$errors = array_merge(array('Záznam nebol zmazaný z databázy.'), Errors::getErrors());
				$message = implode("<br />", $errors);
				$this->setFlashError($message);
			}
		}
		$this->redirect(array('action' => 'index'));
	}
	
}

?>